package com.example.voiceassistant.ai;

import com.example.voiceassistant.config.ConfigManager;
import com.example.voiceassistant.monitoring.MonitoringService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Instant;
import java.util.Locale;

/**
 * AI 回复服务，模拟对接魔搭模型接口。
 */
public class AIResponseService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AIResponseService.class);

    private final ConfigManager configManager;
    private final MonitoringService monitoringService;

    public AIResponseService(ConfigManager configManager, MonitoringService monitoringService) {
        this.configManager = configManager;
        this.monitoringService = monitoringService;
    }

    public String generateReply(String recognizedText, Locale locale) {
        monitoringService.counter("ai.request.count").increment();
        String endpoint = configManager.getAiEndpoint();
        LOGGER.debug("向 AI 模型 [{}] 发起请求，文本: {}", endpoint, recognizedText);

        // 演示返回内容，真实实现需调用魔搭接口。
        String reply;
        if (recognizedText != null && recognizedText.contains("天气")) {
            reply = "当前天气晴朗，气温 22 度，适合出行。";
        } else if (recognizedText != null && recognizedText.contains("灯")) {
            reply = "好的，已经为您打开客厅的灯。";
        } else {
            reply = "已收到您的指令：" + recognizedText + "，我会尽力执行。";
        }

        monitoringService.event("ai.response.generated",
                "reply@" + Instant.now().toString());

        if (Locale.ENGLISH.getLanguage().equals(locale.getLanguage())) {
            return "Simulated reply for: " + recognizedText;
        }

        return reply;
    }
}

